$(function() {

    'use strict';

    var shareSDK   = commonJs.shareSDK;
    var imgLoader  = commonJs.imgLoader;
    var audio      = commonJs.audio;
    var socket     = commonJs.socket;
    var eventPool  = commonJs.eventPool;
    var utils      = commonJs.utils;
    var coverCloud = commonJs.coverCloud;
    var camera     = commonJs.camera;
    var starArea   = commonJs.starArea;
    var girl       = commonJs.girl;

    var cmdControl = {

        _parseCmd: function(cmdString) {
            var cmdArr = cmdString.split('+++');
            return {
                cmd  : cmdArr[0],
                data : cmdArr[1]
            }
        },

        stop: function() {
            socket.stopListening();
        },

        start: function() {

            var that = this;

            socket.listen(function(cmdString) {
                var cmdObj = that._parseCmd(cmdString),
                    cmd    = cmdObj.cmd,
                    data   = cmdObj.data;

                if (cmd === 'reload') {
                    window.location.reload();

                } else if (cmd === 'play') {

                    if (data === 'SOCK_STAR_STAR_EXIT') {
                        eventPool.trigger('CMD_STAR_EXIT');

                    } else if (data.match(/SOCK_OP_W2_SELECT_/)) {
                        eventPool.trigger('CMD_W2_SELECT', data.substr(18));

                    } else if (data.match(/SOCK_SEA_EXIT/)) {
                        eventPool.trigger('CMD_SEA_EXIT');

                    } else {
                        // Do nothing
                    }

                } else {
                    // Do nothing
                }
            });

            return this;
        },

        init: function(callback) {

            socket.init(function() {
                socket.send('connect', utils.getQueryString('id'));
                callback && callback();
            });

            eventPool.one('STAR_START_PLAY', function() {
                socket.send('play', 'SOCK_STAR_START_PLAY');
            }, this);

            eventPool.one('STAR_STAR_ACCROSS', function() {
                socket.send('play', 'SOCK_STAR_STAR_ACCROSS');
            }, this);

            eventPool.one('OP_DONE_W1', function() {
                socket.send('play', 'SOCK_OP_DONE_W1');
            }, this);

            eventPool.on('OP_W2_SELECT', function(data) {
                socket.send('play', 'SOCK_OP_W2_SELECT_' + data);
            }, this);

            eventPool.one('SEA_EXIT', function() {
                socket.send('play', 'SOCK_SEA_EXIT');
            }, this);

            return this;
        }
    };

    var starPage = {

        _el: $('.wrapper .page-star'),

        exit: function(callback) {
            var that = this;
            girl.flyOut(3000);
            utils.moveItem(this._el.find('.scene'), 1240, 3000, function() {
                utils.hideItem(that._el, 1000, function() {
                    that._el.remove();
                });
                callback && callback();
            });
            return this;
        },

        flyAccorss: function(callback) {
            setTimeout(function() {
                girl.reset().flyIn(5000);
            }, 1000);
            utils.moveItem(this._el.find('.scene'), 1040, 6000, function() {
                callback && callback();
            });
            return this;
        },

        showDot: function(callback) {
            utils.showItem(this._el.find('.dot'), 1000);
            return this;
        },

        hideCurtain: function(callback) {
            var that = this;
            utils.hideItem(this._el.find('.star-tip, .drag-tip'), 1000, function() {
                that._el.find('.curtain').attr('style',
                        '-webkit-transform:translateY(-1136px);' +
                        '-webkit-transition:all 1s;');
                setTimeout(function() {
                    callback && callback();
                }, 1000);
            });
            return this;
        },

        showStarAndTip: function(callback) {
            starArea.show();
            utils.showItem(this._el.find('.star-tip'), 1000, function() {
                callback && callback();
            });
            return this;
        },

        init: function() {

            var that = this;

            utils.moveItem(this._el.find('.scene'), 640);

            this.showStarAndTip();

            eventPool.one('STAR_START_PLAY', function() {
                this.hideCurtain(function() {
                    that.showDot();
                });
            }, this);

            eventPool.one('STAR_STAR_ACCROSS', function() {
                this.flyAccorss();
            }, this);

            eventPool.one('CMD_STAR_EXIT', function() {
                this.exit(function() {
                    eventPool.trigger('STAR_STAR_END');
                });
            }, this);

            this._el.one('touchmove', function() {
                eventPool.trigger('STAR_START_PLAY');
                return false;
            });

            this._el.find('.dot').click(function() {
                utils.hideItem($(this), 1000, function() {
                    eventPool.trigger('STAR_STAR_ACCROSS');
                });
            });

            return this;
        }
    };

    var seaPage = {

        _el      : $('.wrapper .page-sea'),
        _sceneEl : $('.wrapper .page-sea .scene'),

        playFirework: function(callback) {
            var that = this;
            utils.hideItem(this._el.find('.word'), 2000, function() {
                that._el.find('.yanhua').show().addClass('show');
            });
            setTimeout(function() {
                utils.hideItem(that._el, 1000, function() {
                    $('.wrapper').remove();
                });
                callback && callback();
            }, 8000);
            return this;
        },

        showWords: function(callback) {
            starArea.hide();
            camera.zoomIn(this._el, 1.5, 50, 0, 2);
            utils.showItem(this._el.find('.word'), 2000);
            setTimeout(function() {
                callback && callback();
            }, 2000);
            return this;
        },

        flyToZhongguo: function(callback) {
            utils.moveItem(this._sceneEl, 8325, 15000, function() {
                callback && callback();
            });
            girl.flyOut(25000);
            return this;
        },

        flyToTaiwan: function(callback) {
            utils.moveItem(this._sceneEl, 5310, 15000, function() {
                callback && callback();
            });
            girl.reset().change();
            setTimeout(function() {
                girl.flyIn(12500);
            }, 2500);
            return this;
        },

        flyToHanguo: function(callback) {
            utils.moveItem(this._sceneEl, 2520, 10000, function() {
                callback && callback();
            });
            return this;
        },

        init: function() {

            var that = this;

            utils.moveItem(this._sceneEl, 640);

            eventPool.one('STAR_STAR_END', function() {
                this.flyToHanguo(function() {
                    eventPool.trigger('SEA_ARRIVE_HANGUO');
                });
            }, this);

            eventPool.one('OP_DONE_W1', function() {
                this.flyToTaiwan(function() {
                    eventPool.trigger('SEA_ARRIVE_TAIWAN');
                });
            }, this);

            eventPool.one('OP_DONE_W2', function() {
                this.flyToZhongguo(function() {
                    eventPool.trigger('SEA_ARRIVE_ZHONGGUO');
                });
            }, this);

            eventPool.one('SEA_PLAY_FIREWORK', function() {
                this.playFirework();
            }, this);

            eventPool.one('OP_DONE_W3', function() {
                this.showWords(function() {
                    that._selfReady = true;
                    eventPool.trigger('SEA_EXIT');

                    if (that._friendReady) {
                        eventPool.trigger('SEA_PLAY_FIREWORK');
                    }
                });
            }, this);

            eventPool.one('CMD_SEA_EXIT', function() {
                if (this._selfReady) {
                    eventPool.trigger('SEA_PLAY_FIREWORK');
                } else {
                    this._friendReady = true;
                }
            }, this);

            return this;
        }
    };

    // Init
    starPage.init();
    seaPage.init();

    var loadList = [],
        list = $('#preload_list img');

    for (var i = list.length; i--;) {
        loadList.push(list.eq(i).data('src'));
    }

    imgLoader.reset(loadList, function() {

        cmdControl.init(function() {
            cmdControl.start();

            $('#loading-cover').fadeOut(1000, function() {
                $('#loading-cover').remove();
            });
        });

    }).load();
});
